const fs = require('fs');
const cheerio = require('cheerio');
const axios = require('axios').default;

const books = [];

axios
    .get('https://top.kanshu.com/')
    .then(res => {
        const $ = cheerio.load(res.data);
        $('.booksBox')
            .find('a')
            .each(function (index) {
                const book = {};
                book.id = index;
                book.title = $(this).find('.book-info .book-title').text();
                book.link = $(this).attr('href');
                fs.mkdirSync('./books/' + book.title, {
                    recursive: true
                });
            })
        books.push(book);
    });
fs.writeFileSync('./book.json', JSON.stringify(books));